/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package form;

import core.event.AnimationEvent;
import core.event.AnimationListener;
import core.world.WorldAction;
import core.world.Animation;
import io.resource.ResourceDelegate;
import io.resource.ResourceReader;
import java.awt.Window;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;

/**
 *
 * @author Robert A. Cherry
 */
public class AnimationBinder extends javax.swing.JDialog implements AnimationListener {

    // Variable Declaration
    // Project Classes
    private Animation animation;
    private ImagePanel panel;
    private ResourceDelegate delegate;
    private WorldAction action;
    // End of Variable Declaration

    public AnimationBinder(Window window, ResourceDelegate delegate, WorldAction action, Animation animation, boolean modal) {

        //
        super(window);
        setModal(modal);
        initComponents();

        // Set values
        this.action = action;
        this.animation = animation;
        this.delegate = delegate;

        //
        init();
    }

    private void init() {

        //
        setupActions();

        //
        panel = new ImagePanel(renderJScrollPane);

        //
        renderJScrollPane.setViewportView(panel);

        if (animation != null) {

            //
            animation.addAnimationListener(this);
        }

        //
        final Class closs = getClass();

        //
        final ImageIcon icon = ResourceReader.readClassPathIcon(closs, "/icons/icon-animation24.png");

        //
        //animationJButton.setFocusable(false);
        animationJButton.setIcon(icon);

        // Change title
        setTitle("Binding Animation to Action: " + makeReadable(action.name()));
    }

    private void finish() {

        //
        setVisible(false);
    }

    private void setupActions() {

        //
        final DefaultComboBoxModel model = new DefaultComboBoxModel();

        // Grab every action that exists
        final WorldAction[] actions = WorldAction.values();

        // Consider each action
        for (int i = 0; i < actions.length; i++) {

            //
            final WorldAction iAction = actions[i];

            //
            model.addElement(iAction);
        }

        // Apply the model
        actionJComboBox.setModel(model);

        // Attempt to set default selected
        if (action != null) {
            actionJComboBox.setSelectedItem(action);
        }
    }

    private String makeReadable(String string) {

        //
        if (string == null || string.isEmpty()) {
            return string;
        }

        // Make the first letter Capital
        char c = string.charAt(0);

        // If c is a lowercase letter
        if (c >= 97 && c <= 122) {

            // Make it capital
            c -= 32;
        }

        //
        string = String.valueOf(c).concat( string.substring(1, string.length()).toLowerCase() );
        
        //
        return string;
    }

    @Override
    public void animationStep(AnimationEvent event) {
        panel.repaint();
    }

    @Override
    public void animationEnd(AnimationEvent event) {
        panel.repaint();
    }

    public WorldAction getAction() {
        return action;
    }

    public Animation getAnimation() {
        return animation;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        renderJScrollPane = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        animationJButton = new javax.swing.JButton();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        finishJButton = new javax.swing.JButton();
        filler3 = new javax.swing.Box.Filler(new java.awt.Dimension(8, 0), new java.awt.Dimension(8, 0), new java.awt.Dimension(8, 32767));
        cancelJButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        actionJLabel = new javax.swing.JLabel();
        filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        actionJComboBox = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(285, 302));
        setMinimumSize(new java.awt.Dimension(285, 302));
        setModal(true);
        setResizable(false);

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));

        animationJButton.setToolTipText("Select an Animation");
        animationJButton.setMaximumSize(new java.awt.Dimension(26, 26));
        animationJButton.setMinimumSize(new java.awt.Dimension(26, 26));
        animationJButton.setPreferredSize(new java.awt.Dimension(26, 26));
        animationJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                animationJButtonActionPerformed(evt);
            }
        });
        jPanel1.add(animationJButton);
        jPanel1.add(filler1);

        finishJButton.setText("Finish");
        finishJButton.setMaximumSize(new java.awt.Dimension(88, 26));
        finishJButton.setMinimumSize(new java.awt.Dimension(88, 26));
        finishJButton.setPreferredSize(new java.awt.Dimension(88, 26));
        finishJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                finishJButtonActionPerformed(evt);
            }
        });
        jPanel1.add(finishJButton);
        jPanel1.add(filler3);

        cancelJButton.setText("Cancel");
        cancelJButton.setMaximumSize(new java.awt.Dimension(88, 26));
        cancelJButton.setMinimumSize(new java.awt.Dimension(88, 26));
        cancelJButton.setPreferredSize(new java.awt.Dimension(88, 26));
        cancelJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelJButtonActionPerformed(evt);
            }
        });
        jPanel1.add(cancelJButton);

        jPanel2.setMaximumSize(new java.awt.Dimension(265, 22));
        jPanel2.setMinimumSize(new java.awt.Dimension(256, 26));
        jPanel2.setPreferredSize(new java.awt.Dimension(256, 26));
        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.LINE_AXIS));

        actionJLabel.setText("Bind Animation to Action:");
        actionJLabel.setMaximumSize(new java.awt.Dimension(128, 26));
        actionJLabel.setMinimumSize(new java.awt.Dimension(128, 26));
        actionJLabel.setPreferredSize(new java.awt.Dimension(128, 26));
        jPanel2.add(actionJLabel);
        jPanel2.add(filler2);

        actionJComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        actionJComboBox.setEnabled(false);
        actionJComboBox.setMaximumSize(new java.awt.Dimension(128, 26));
        actionJComboBox.setMinimumSize(new java.awt.Dimension(128, 26));
        actionJComboBox.setPreferredSize(new java.awt.Dimension(128, 26));
        jPanel2.add(actionJComboBox);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(renderJScrollPane)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(renderJScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void finishJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_finishJButtonActionPerformed
        // Save changes
        finish();
    }//GEN-LAST:event_finishJButtonActionPerformed

    private void cancelJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelJButtonActionPerformed
        //
        action = null;
        animation = null;

        // Close Dialog
        setVisible(false);
    }//GEN-LAST:event_cancelJButtonActionPerformed

    private void animationJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_animationJButtonActionPerformed

        // Show the script selector
        final ResourceSelector maker = new ResourceSelector(this, delegate, true);
        maker.setFilterType(Animation.class);
        maker.setLocationRelativeTo(this);
        maker.setVisible(true);

        // Dispose of the dialog
        maker.dispose();

        // Attempt to remove its previous listener
        if (animation != null) {
            animation.removeAnimationListener(this);
        }

        //
        animation = (Animation) maker.getResource();

        // Attempt to set its new listener as this dialog
        if (animation != null) {
            animation.addAnimationListener(this);
        }

        //
        panel.repaint();
    }//GEN-LAST:event_animationJButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox actionJComboBox;
    private javax.swing.JLabel actionJLabel;
    private javax.swing.JButton animationJButton;
    private javax.swing.JButton cancelJButton;
    private javax.swing.Box.Filler filler1;
    private javax.swing.Box.Filler filler2;
    private javax.swing.Box.Filler filler3;
    private javax.swing.JButton finishJButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane renderJScrollPane;
    // End of variables declaration//GEN-END:variables
}
